import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'index',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [

      { path: 'firstPage', component: () => import('../views/FirstPageView.vue') },
      {
        path: 'role', component: () => import('../views/RoleView.vue'),
        children: [
          {
            path: '', name: 'RoleMember', component: () => import('../components/RoleMember.vue')
          },
          {
            path: '/home/role/roleNewMember', name: 'RoleNewMember', component: () => import('../components/RoleNewMember.vue')
          },
          {
            path: '/home/role/roleMemberInfo', name: 'roleMemberInfo', component: () => import('../components/RoleMemberInfo.vue')
          }
        ]
      },
      {
        path: 'department', component: () => import('../views/DepartmentView.vue')
      },
      { path: 'administrantors', component: () => import('../views/AdministratorsView.vue') },
      { path: 'platformInfo', component: () => import('../views/PlatformInfoView.vue') },
      { path: 'parameters', component: () => import('../views/ParametersView.vue') },
      {
        path: 'feedback', component: () => import('../views/FeedbackView.vue'),
        // children: [{ path: '/home/feedback/feedbackDetails', component: () => import('../components/FeedbackDetailsView.vue') }]
      },
      { path: '/home/feedback/feedbackDetails/:id', name: 'feedbackDetails', component: () => import('../components/FeedbackDetailsView.vue') },
      { path: 'inspection', component: () => import('../views/InspectionView.vue') },
      { path: 'userManagement', component: () => import('../views/UserManagementView.vue') },
      { path: 'userDetails', component: () => import('../views/UserDetails.vue') },
      // { path: 'platformInfoView', component: () => import('../views/PlatformInfoView.vue') },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login


  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach(to => {
  const token: string | null = localStorage.getItem('token')
  if (!token && to.path !== "/login") {
    alert("请先登录")
    return '/login'
  }
})

export default router
